<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use App\Models\Tixian;

class TixianController extends Controller
{
      public function index(){
        //总收入
//        $total = DB::table('order')->where('pay_status',1)->sum('price');
        //总提现
        $tixian = round(DB::table('tixian')->where('status',1)->sum('price'),2);
//        //当天收入
//        $toshou = DB::table('order')
//        ->where('pay_status',1)
//        ->where('pay_time','>',strtotime("-1 day"))
//        ->sum('price');

         $data = DB::table('tixian')->orderBy('id', 'desc')->paginate(15);
         return view('admin.tixian.index',compact('data','total','tixian','toshou'));
      }

      //编辑视图
      public function edit($id){


      $data = DB::table('tixian')->where('id',$id)->first();
        return view('admin.tixian.edit',compact('data'));
      }



//更新提交
    public function update(Request $request,$id){

      $res = DB::table('tixian')->where('id',$id)->update([
            'status' => $request->status,
        ]);
      if ($res == 1) {
         return redirect('admin/tixian');
      }else{
        return back()->with('更新失败！');
      }
    }

//搜索提交
    public function store(Request $request){
          $search = $request->keyword;
          $data = DB::table('tixian')->where('openid', 'like', '%'.$search.'%')->get();
          return view('admin.tixian.index',compact('data'));
    }

    //退款管理
    public function tui(){
          $data = DB::table('tui')
              ->leftJoin('redf','tui.redid','=','redf.id')
              ->orderBy('tui.created_at', 'desc')
              ->select('tui.id','tui.created_at','tui.redid','tui.openid','tui.price','redf.sign')
              ->paginate(15);
        $refundSum = round(DB::table('tui')->sum('price'),2);
         return view('admin.tui.index',compact('data','refundSum'));
    }
}
